<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const isLoggedIn = ref(false);
const showUserMenu = ref(false);

// 检查登录状态
onMounted(() => {
  isLoggedIn.value = localStorage.getItem('isLoggedIn') === 'true';
});

// 切换用户菜单
const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value;
};

// 退出登录
const logout = () => {
  localStorage.removeItem('isLoggedIn');
  isLoggedIn.value = false;
  showUserMenu.value = false;
  router.push('/');
};

// 点击外部关闭菜单
const closeUserMenu = (event: MouseEvent) => {
  const target = event.target as HTMLElement;
  if (!target.closest('.user-menu-container')) {
    showUserMenu.value = false;
  }
};

// 添加事件监听
onMounted(() => {
  document.addEventListener('click', closeUserMenu);
});
</script>

<template>
  <header class="header">
    <div class="container">
      <div class="header-content">
        <div class="logo">logo</div>
        <nav class="nav">
          <router-link to="/" class="nav-item">首页</router-link>
          <router-link to="/destination" class="nav-item">目的地</router-link>
          <router-link to="/guide" class="nav-item">攻略</router-link>
          <router-link to="/hotel" class="nav-item">酒店</router-link>
          <router-link to="/flight" class="nav-item">机票</router-link>
        </nav>
        <div class="auth-buttons">
          <!-- 未登录状态 -->
          <template v-if="!isLoggedIn">
            <router-link to="/login" class="login-button">登录</router-link>
            <router-link to="/register" class="register-button">注册</router-link>
          </template>
          
          <!-- 已登录状态 -->
          <div v-else class="user-menu-container">
            <div class="user-avatar" @click="toggleUserMenu">
              <img src="https://tse2-mm.cn.bing.net/th/id/OIP-C.7GLMYPqMlt2LgkbPsOnDIAAAAA?rs=1&pid=ImgDetMain" alt="用户头像">
            </div>
            <div class="user-menu" v-if="showUserMenu">
              <router-link to="/user" class="menu-item">
                <i class="icon">👤</i>
                <span>个人中心</span>
              </router-link>
              <router-link to="/user?tab=orders" class="menu-item">
                <i class="icon">🧾</i>
                <span>我的订单</span>
              </router-link>
              <router-link to="/user?tab=favorites" class="menu-item">
                <i class="icon">❤️</i>
                <span>我的收藏</span>
              </router-link>
              <div class="menu-item" @click="logout">
                <i class="icon">🚪</i>
                <span>退出登录</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<style scoped>
.header {
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  width: 100%;
}

.container {
  max-width: 1800px;
  margin: 0 auto;
  padding: 0 20px;
}

.header-content {
  display: flex;
  align-items: center;
  padding: 15px 0;
  position: relative;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  color: #0066cc;
  flex: 0 0 auto;
}

.nav {
  display: flex;
  gap: 30px;
  margin-left: 50px;
  flex: 1;
}

.nav-item {
  cursor: pointer;
  font-size: 16px;
}

.auth-buttons {
  display: flex;
  gap: 10px;
  position: absolute;
  left: 500px;
  flex: 0 0 auto;
}

.login-button {
  padding: 3px 15px;
  border: 1px solid #0066cc;
  background-color: white;
  color: #0066cc;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
}

.login-button:hover {
  background-color: #f0f8ff;
}

.register-button {
  padding: 3px 15px;
  border: none;
  background-color: #0066cc;
  color: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
}

.register-button:hover {
  background-color: #0055aa;
}

.user-menu-container {
  position: relative;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid #f0f8ff;
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-menu {
  position: absolute;
  top: 50px;
  right: 0;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
  width: 180px;
  z-index: 100;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  color: #333;
  text-decoration: none;
  transition: background-color 0.2s;
}

.menu-item:hover {
  background-color: #f0f8ff;
}

.menu-item .icon {
  margin-right: 10px;
}
</style>